/** 创建组件对象 * */
var groupForm = new Form();
var groupTable = new Table();
var groupPage = new Page();
var groupDialog = new Dialog();
var groupAjax = new Ajax();
var indicatorGroupTree = new Tree();
var indicatorForm = new Form();
var indicatorTable = new Table();
var indicatorPage = new Page();
var indicatorAjax = new Ajax();
var isSysAdmin = false;
var flag = false;
var currentWindow = "";
var majorObj = {};

/** 页面加载完执行 * */
$(function() {
	isSysAdmin = sessionStorage.user_loginId == "sysadmin";
	flag = sessionStorage.orgCode == "*";
	initScript();
	initButtons();
	initIndicatorGroupTree();
	initTable();
	groupPageQuery("root_root");
	initElementsControl();
	resetTableHeight();

	sessionStorage.parentId = "root";
	sessionStorage.nodeId = "root_root";
	sessionStorage.leaf = false;
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/indicator/locale/Indicator_" + appLocale + ".js"); // 1.加载国际化js*
	loadScript(appPath + "/project-yddemo/indicator/locale/IndicatorGroup_" + appLocale + ".js"); // 1.加载国际化js*
	loadScript(appPath + "/project-yddemo/indicator/properties/IndicatorProperties.js");
	loadScript(appPath + "/project-yddemo/indicator/properties/IndicatorGroupProperties.js");
}

/** 初始化按钮 * */
function initButtons() {
	groupTable.initButtons({
		divId : "indicatorGroupButtonDiv", // 1.归属div*
		buttons : groupProperties.getViewButtons()
	});
	indicatorTable.initButtons({
		divId : "indicatorButtonDiv", // 1.归属div*
		buttons : indicatorProperties.getViewButtons()
	});
	if (flag && !isSysAdmin) {
		setElementsEnabled("yddemo:indicatorGroup:add", "button", false);
	}
	disableButton();
	disableIndicatorButton();
}

function disableButton() {
	setElementsEnabled("yddemo:indicatorGroup:edit", "button", false);
	setElementsEnabled("yddemo:indicatorGroup:delete", "button", false);
	setElementsEnabled("yddemo:indicator:add", "button", false);
	// setElementsEnabled("yddemo:indicator:import", "button", false);
	// setElementsEnabled("yddemo:indicator:export", "button", false);
}

function disableIndicatorButton() {
	setElementsEnabled("yddemo:indicator:edit", "button", false);
	setElementsEnabled("yddemo:indicator:delete", "button", false);
}

function enableButton() {
	setElementsEnabled("yddemo:indicatorGroup:edit", "button", true);
	setElementsEnabled("yddemo:indicatorGroup:delete", "button", true);
	setElementsEnabled("yddemo:indicator:add", "button", true);
	// setElementsEnabled("yddemo:indicator:import", "button", true);
	// setElementsEnabled("yddemo:indicator:export", "button", true);
	$("[action='yddemo:indicatorGroup:edit']").unbind("click").bind("click", function() {
		updateIndicatorGroup();
	});
	$("[action='yddemo:indicatorGroup:delete']").unbind("click").bind("click", function() {
		deleteIndicatorGroup();
	});
	$("[action='yddemo:indicator:add']").unbind("click").bind("click", function() {
		addIndicator();
	});
	// $("[action='yddemo:indicator:import']").unbind("click").bind("click",
	// function() {
	// importDictEntrys();
	// });
	// $("[action='yddemo:indicator:export']").unbind("click").bind("click",
	// function() {
	// exportDictEntrys();
	// });
}

function enableIndicatorButton() {
	setElementsEnabled("yddemo:indicator:edit", "button", true);
	setElementsEnabled("yddemo:indicator:delete", "button", true);
	$("[action='yddemo:indicator:edit']").unbind("click").bind("click", function() {
		updateIndicator();
	});
	$("[action='yddemo:indicator:delete']").unbind("click").bind("click", function() {
		deleteIndicator();
	});
}

function initTable() {
	indicatorTable.initTable({
		divId : "indicatorTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/yddemo/indicators", // 请求url*
			type : "GET"// , // 请求类型*
		// page: dictEntryPage, //分页对象(需要分页时配置)
		// models : indicatorProperties.getViewModels()
		},
		columns : indicatorProperties.getViewColumns(), // 3.表格列配置
		rowClickFun : function(data) {
			var selectDatas = indicatorTable.getSelectDatas();
			if (selectDatas.length == 1) {
				enableIndicatorButton();
			} else if (selectDatas.length > 1) {
				enableIndicatorButton();
				setElementsEnabled("yddemo:indicator:edit", "button", false);
			} else {
				disableIndicatorButton();
			}
		}
	});
}

function initIndicatorGroupTree() {
	indicatorGroupTree.initTree({
		divId : "indicatorGroupTreeDiv", // 归属div*
		ajaxType : "param", // ajax:初始化url/root_root,懒加载url/nodeId);
		// param:初始化url?node=root_root,懒加载url?node=nodeId
		root : {
			text : groupLocale.text.groupRoot,
			nodeId : "root_root",
			leaf : false,
			expanded : true
		},
		dataAjax : { // 数据源ajax*
			url : requestPath + "/yddemo/indicatorGroupTree", // 请求url*
			async : false, // 是否异步*
			type : "GET", // 请求类型*
			params : {},
			data : {}
		},
		align : { // 元素顺序
			left : "expand",
			center : "text",
			right : "check"
		},
		checkIcon : staticPath + "/framework/default/img/Icon_tree_check.png", // 选中图标
		partcheckIcon : staticPath + "/framework/default/img/Icon_tree_partcheck.png", // 部分选图标
		uncheckIcon : staticPath + "/framework/default/img/Icon_tree_uncheck.png", // 未选图标
		expandIcon : staticPath + "/framework/default/img/Icon_tree_expand.png", // 展开图标
		closeIcon : staticPath + "/framework/default/img/Icon_tree_close.png", // 闭合图标
		textClickFun : function(nodeId) { // 文本单击事件*
			// 文本单击事件只操作右边div的显示的tab项
			if (nodeId == "undefined") {
				nodeId = "root_root";
			}
			indicatorGroupTree.checkDefaultFun(nodeId);// 添加
			groupPageQuery(nodeId);
		},
		expandFun : function(nodeId) { // 展开单击事件*
			// 展开单击事件只对树进行展开和合并操作
			if (nodeId == "undefined") {
				nodeId = "root_root";
			}
			indicatorGroupTree.expandDefaultFun(nodeId);
			groupPageQuery(nodeId);// 添加
		},
		checkFun : function(nodeId) { // 选中单击事件*
			if (nodeId == "undefined") {
				nodeId = "root_root";
			}
			indicatorGroupTree.checkDefaultFun(nodeId);
			groupPageQuery(nodeId);
		}
	});
}

function groupPageQuery(nodeId) {
	var nodeObj;
	if (nodeId != "root_root" && nodeId != "undefined") {
		nodeObj = indicatorGroupTree.getNode(nodeId);
	} else {
		nodeObj = setRootNode();
	}

	sessionStorage.parentId = nodeObj.nodePid;
	sessionStorage.leaf = nodeObj.leaf;
	sessionStorage.nodeId = nodeId;
	var entityType = nodeObj.entityType;

	if (!nodeObj.leaf) {
		// 刷新树，控制按钮，查询右边指标
		disableButton();
		indicatorQuery();
	} else {
		// 是叶子,点击后触发右边指标显示的内容，并且控制按钮的可用状态
		if ("IndicatorGroup" == entityType) {
			sessionStorage.groupId = nodeObj.id;

			if (flag && !isSysAdmin) {
				return;
			}
			enableButton();
			refreshIndicator();
		}
	}
}

function indicatorQuery() {
	var params = {
		groupId : ""
	};
	indicatorTable.reloadData({}, params);
	disableIndicatorButton();
}

function refreshIndicator() {
	var params = {
		groupId : sessionStorage.groupId
	};

	indicatorTable.reloadData({}, params);
	disableIndicatorButton();
}

function updateTreeNode(nodeId) {
	// 刷新树法一
	var nodeObj = indicatorGroupTree.getNode(nodeId);
	nodeObj.expanded = false;
	nodeObj.children = null;
	indicatorGroupTree.reloadTree();
	indicatorGroupTree.expandDefaultFun(nodeId);
}

function setRootNode() {
	var root = {};
	root.entityId = "root";
	root.entityType = "root";
	root.expanded = true;
	root.iconCls = "ApplicationViewTile";
	root.id = "root";
	root.leaf = false;
	root.nodeId = "root_root";
	root.nodePid = "";
	root.params = null;
	root.text = groupLocale.text.groupRoot;
	return root;
}

/** 新增 * */
function addIndicatorGroup() {
	groupDialog.iframeDialog(requestPath + "/yddemo/indicatorGroups/page/add", groupLocale.text.addIndicatorGroup, 400,
			150, "yddemo:indicatorGroup:add");
}

function addIndicator() {
	groupDialog.iframeDialog(requestPath + "/yddemo/indicators/page/add", indicatorLocale.text.addIndicator, 400, 300,
			"yddemo:indicator:add");
}

/** 修改 * */
function updateIndicatorGroup() {
	sessionStorage.selectedId = sessionStorage.groupId;
	groupDialog.iframeDialog(requestPath + "/yddemo/indicatorGroups/page/edit", groupLocale.text.editIndicatorGroup,
			400, 150, "yddemo:indicatorGroup:edit");
}

function updateIndicator() {
	var selectDatas = indicatorTable.getSelectDatas();
	if (selectDatas.length != 1) {
		groupDialog.alertDialog(indicatorLocale.text.selectData);
		return;
	}
	sessionStorage.selectedId = selectDatas[0].id;
	groupDialog.iframeDialog(requestPath + "/yddemo/indicators/page/edit", indicatorLocale.text.editIndicator, 400,
			300, "yddemo:indicator:edit");
}

/** 删除 * */
function deleteIndicatorGroup() {
	sessionStorage.selectedId = sessionStorage.groupId;
	groupDialog.confirmDialog(groupLocale.text.isDelete, function() {
		var resultJson = groupAjax.sendAjax({
			url : requestPath + "/yddemo/indicatorGroups/" + sessionStorage.selectedId, // 请求url*
			async : false, // 是否异步*
			type : "DELETE" // 请求类型*
		});
		if (resultJson.success) {
			groupDialog.tipDialog(groupLocale.text.deleteSuccess, function() {
				groupPageQuery("root_root");
				indicatorQuery();
			});
		} else {
			groupDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});
}

function deleteIndicator() {
	var selectDatas = indicatorTable.getSelectDatas();
	var ids = selectDatas.map(function(item, index, array) {
		return item.id;
	});
	groupDialog.confirmDialog(indicatorLocale.text.isDelete, function() {
		var resultJson = indicatorAjax.sendAjax({
			url : requestPath + "/yddemo/indicators", // 请求url*
			async : false, // 是否异步*
			type : "DELETE", // 请求类型*
			data : ids
		});
		if (resultJson.success) {
			groupDialog.tipDialog(indicatorLocale.text.deleteSuccess, function() {
				refreshIndicator();
			});
		} else {
			groupDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});
}

function resetTableHeight() {
	var leftHeight = $("#indicatorGroupDataListDiv").height() - getTotalHeight([ "indicatorGroupButtonDiv" ]);
	$("#indicatorGroupTreeDiv").height(leftHeight);
	var leftHeight = $("#indicatorDataListDiv").height() - getTotalHeight([ "indicatorButtonDiv" ]);
	$("#indicatorTableDiv").height(leftHeight);
}
